package com.chenglai.classify.controller;

import com.chenglai.classify.pojo.GoodsClassify;
import com.chenglai.classify.service.GoodsClassifyService;
import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2023/11/14 17:37:33
 **/
@RestController
@RequestMapping(value = "/goodsClassify")
@Api(value = "/goodsClassify", tags = {"商品分类"})
@CrossOrigin
public class GoodsClassifyController {

    @Autowired
    private GoodsClassifyService goodsClassifyService;

    @PostMapping("/addGoodsClassify")
    @ApiOperation(value = "添加商品分类")
    public  Result addGoodsClassify(GoodsClassify goodsClassify){
        try {
            goodsClassifyService.addClassify(goodsClassify);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/listGoodsClassify")
    @ApiOperation(value = "获取商品分类列表")
    public  Result listGoodsClassify(@ApiParam(name="shopId",value = "门店ID") Integer shopId,
                                     @ApiParam(name="parentId",value = "上级ID") Integer parentId){
        try {
            List<GoodsClassify> classifies = goodsClassifyService.listGoodsClassify(shopId,parentId);
            return new Result(true, StatusCode.OK,"查询成功",classifies);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @GetMapping("/classifyDetail")
    @ApiOperation(value = "编辑商品分类")
    public  Result classifyDetail(@ApiParam(name="classifyId", value="分类ID")Integer classifyId){
        try {

            GoodsClassify classify = goodsClassifyService.classifyDetail(0);
            return new Result(true, StatusCode.OK,"查询成功",classify);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/deleteClassify")
    @ApiOperation(value = "删除商品分类")
    public  Result deleteClassify(@ApiParam(name="classifyId", value="分类ID")Integer classifyId,
                                  @ApiParam(name="classifyType", value="分类登记")Integer classifyType){
        try {
            Map map = goodsClassifyService.deleteClassify(classifyId,classifyType);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @PostMapping("/addClassifyPics")
    @ApiOperation(value = "批量添加商品分类图片库")
    public  Result addClassifyPics(HttpServletRequest request){
        try {
            goodsClassifyService.addClassifyPics(request);
            return new Result(true, StatusCode.OK,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @PostMapping("/delClassifyPic")
    @ApiOperation(value = "批量删除商品分类图片")
    public  Result delClassifyPic(@ApiParam(name="ids", value="图片ids")String ids){
        try {
            goodsClassifyService.delClassifyPic(ids);
            return new Result(true, StatusCode.OK,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }



}
